package family;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class Ask extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;
    JLabel jlpno = new JLabel("工      号：");
    JLabel jlname = new JLabel("姓      名：");
    JLabel jlsex = new JLabel("性      别：");
    JLabel jlage = new JLabel("年      龄：");
    JLabel jlrelation = new JLabel("与本人关系：");
    JLabel jllocation = new JLabel("工 作 地 点：");
    JLabel jlphone = new JLabel( "联 系 电 话：");
    JLabel jlwork = new JLabel("职      务：");

    JTextField jtpno = new JTextField("",20);

    JLabel jname = new JLabel();
    JLabel jsex = new JLabel();
    JLabel jage = new JLabel();
    JLabel jrelation = new JLabel();
    JLabel jlocation = new JLabel();
    JLabel jphone = new JLabel();
    JLabel jwork = new JLabel();

    JButton buttonask = new JButton("查询");
    JButton buttonreturn = new JButton("返回");

    public Ask() {
        JPanel jppno = new JPanel();
        JPanel jpname = new JPanel();
        JPanel jpsex = new JPanel();
        JPanel jpage = new JPanel();
        JPanel jprelation = new JPanel();
        JPanel jplocation = new JPanel();
        JPanel jpphone = new JPanel();
        JPanel jpwork = new JPanel();
        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jppno.add(jlpno);
        jppno.add(jtpno);

        jpname.add(jlname);
        jpname.add(jname);

        jpsex.add(jlsex);
        jpsex.add(jsex);

        jpage.add(jlage);
        jpage.add(jage);

        jprelation.add(jlrelation);
        jprelation.add(jrelation);

        jplocation.add(jllocation);
        jplocation.add(jlocation);

        jpphone.add(jlphone);
        jpphone.add(jphone);

        jpwork.add(jlwork);
        jpwork.add(jwork);


        jpforbutton.add(buttonask);
        jpforbutton.add(buttonreturn);
        buttonask.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                Connection conn = null;
                ResultSet res = null;
                Statement stat = null;

                String sql = "SELECT pno,name,sex,age,relation,age,location,phone,zhiwu FROM family;";
                try{
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    System.out.println("jdbc haha");
                }catch(Exception d){
                    System.out.println("jdbc fall");
                    d.printStackTrace();
                }
                try{
                    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/work","root","123456");
                    stat=conn.createStatement();
                    res=stat.executeQuery(sql);
                    while (res.next()) {
                        String i = res.getString(1).trim();

                        if (i.equals(jtpno.getText())) {
                            jname.setText(res.getString(2));
                            jsex.setText(res.getString(3));
                            jage.setText(res.getString(4));
                            jrelation.setText(res.getString(5));
                            jlocation.setText(res.getString(6));
                            jphone.setText(res.getString(7));
                            jwork.setText(res.getString(8));
                            break;
                        } else {
                            if (e.getSource() == buttonask) {
                                javax.swing.JOptionPane.showMessageDialog(null, "不存在！查询失败！");
                            }
                            break;
                        }
                    }
                }catch (SQLException e1) {
                    e1.printStackTrace();


                }
                finally{
                    try{
                        conn.close();

                    }catch(SQLException ar){
                        ar.printStackTrace();
                    }

                }}}

        );
        buttonreturn.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){dispose();
            }
        });
        this.setTitle("教师家庭关系");
        this.setLayout(new GridLayout(9,1));
        this.add(jppno);
        this.add(jpname);
        this.add(jpsex);
        this.add(jpage);
        this.add(jprelation);
        this.add(jplocation);
        this.add(jpphone);
        this.add(jpwork);

        this.add(jpforbutton);
        this.setLocation(400,300);
        this.setSize(350,300);
        this.setVisible(true);

    }
}
